/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.shishuo.cms.constant.SystemConstant;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.AdminService;
import com.shishuo.cms.util.HttpUtils;

/**
 * @author Herbert
 */

@Controller
@RequestMapping("/admin")
public class AdminAction extends BaseAction {
    private static Logger logger = Logger.getLogger(AdminAction.class);
    /**
     * Kaptcha 验证码
     */
    @Autowired
    private DefaultKaptcha captchaProducer;

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String login(HttpServletRequest request, ModelMap modelMap) {
        logger.info("用户登陆：{}"+modelMap.toString());
        return "/manage/login";
    }

    @RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
    public String adminLogout(HttpServletRequest request, ModelMap modelMap) {
        request.getSession().removeAttribute(SystemConstant.SESSION_ADMIN);
        return "redirect:" + HttpUtils.getBasePath(request);
    }

    /**
     * 结果对象来处理成功，失败的结果
     * 错误信息用map来收集
     *
     * @param name
     * @param password
     * @param captcha
     * @param request
     * @param modelMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login.json", method = RequestMethod.POST)
    public JsonVo<String> adminLogin(@RequestParam(value = "name") String name,
                                     @RequestParam(value = "password") String password,
                                     @RequestParam(value = "captcha") String captcha,
                                     HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();

        try {
            String kaptcha = (String) request.getSession().getAttribute(
                    com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
            if (StringUtils.isBlank(password)) {
                json.getErrors().put("password", "密码不能为空");
            } else if (password.length() < 6 && password.length() > 30) {
                json.getErrors().put("password", "密码最少6个字符，最多30个字符");
            }
            // 校验验证码
            if (StringUtils.isNotBlank(kaptcha)
                    && kaptcha.equalsIgnoreCase(captcha)) {
            //验证成功不做处理，失败抛出错误信息
            } else {
                json.getErrors().put("captcha", "验证码错误");
            }
            //判断结果，没有收集到结果信赋值true,否则赋值false
            json.check();

            adminService.adminLogin(name, password, request);

        } catch (Exception e) {
            // 异常，重置验证码
            request.getSession().removeAttribute(
                    com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
            json.setResult(false);
            json.getErrors().put("password", "邮箱或密码错误");
            json.setMsg("change_captcha");
        }
        return json;
    }

    /**
     * 生成验证码
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "captcha.htm", method = RequestMethod.GET)
    public void captcha(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();
        request.getSession().setAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}
